//
//  PersonalInfoViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/22.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "PersonalInfoViewController.h"
#import "PersonalInfoTopTableViewCell.h"
#import "PersonalInfoMiddleTableViewCell.h"
#import "PersonalInfoBottomTableViewCell.h"
#import "SexView.h"
#import "BirthDayView.h"
@interface PersonalInfoViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSMutableArray * titleArr;
@property (nonatomic, strong)SexView * sexView;
@property (nonatomic, strong)BirthDayView * birthDayView;
@end

@implementation PersonalInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"个人信息";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    //右侧消息按钮
    UIButton * naviRightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40/WIDTH_6_SCALE, 20/WIDTH_6_SCALE)];
    [naviRightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [naviRightBtn setTitle:@"跳过" forState:UIControlStateNormal];
    [naviRightBtn addTarget:self action:@selector(naviRightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    naviRightBtn.titleLabel.font = DEF_FontSize_12;
    UIBarButtonItem * rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviRightBtn];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    //提交按钮
    UIButton * submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(20/WIDTH_6_SCALE, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT - 60/WIDTH_6_SCALE, SCREEN_WIDTH - 40/WIDTH_6_SCALE, 40/WIDTH_6_SCALE)];
    [submitBtn setBackgroundImage:[UIImage imageNamed:@"maself_bg"] forState:UIControlStateNormal];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    submitBtn.titleLabel.font = DEF_FontSize_14;
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitBtn.layer.cornerRadius = 4;
    submitBtn.layer.masksToBounds = YES;
    [self.view addSubview:submitBtn];
}
#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 100/WIDTH_6_SCALE;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[PersonalInfoTopTableViewCell class] forCellReuseIdentifier:NSStringFromClass([PersonalInfoTopTableViewCell class])];
        [_tableView registerClass:[PersonalInfoMiddleTableViewCell class] forCellReuseIdentifier:NSStringFromClass([PersonalInfoMiddleTableViewCell class])];
        [_tableView registerClass:[PersonalInfoBottomTableViewCell class] forCellReuseIdentifier:NSStringFromClass([PersonalInfoBottomTableViewCell class])];
        _tableView.backgroundColor = [UIColor whiteColor];
//        //提交按钮
//        UIButton * submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(20/WIDTH_6_SCALE, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT - 60/WIDTH_6_SCALE, SCREEN_WIDTH - 40/WIDTH_6_SCALE, 40/WIDTH_6_SCALE)];
//        [submitBtn setBackgroundImage:[UIImage imageNamed:@"maself_bg"] forState:UIControlStateNormal];
//        [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
//        submitBtn.titleLabel.font = DEF_FontSize_14;
//        [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        submitBtn.layer.cornerRadius = 4;
//        submitBtn.layer.masksToBounds = YES;
//        [_tableView addSubview:submitBtn];
    }return _tableView;
}
- (NSMutableArray *)titleArr{
    if (!_titleArr) {
        _titleArr = @[@{@"title":@"头像"},
                      @{@"title":@"昵称"},
                      @{@"title":@"出生年月"},
                      @{@"title":@"性别"}].mutableCopy;
    }return _titleArr;
}
- (SexView *)sexView{
    if (!_sexView) {
        _sexView = [[SexView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT )];
    }return _sexView;
}
- (BirthDayView *)birthDayView{
    if (!_birthDayView) {
        _birthDayView = [[BirthDayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }return _birthDayView;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    if (indexPath.row == 0) {
        PersonalInfoTopTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalInfoTopTableViewCell class]) forIndexPath:indexPath];
        cell.model = self.titleArr[indexPath.row];
        return cell;
    }
    if (indexPath.row == 1) {
        PersonalInfoMiddleTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalInfoMiddleTableViewCell class]) forIndexPath:indexPath];
        cell.model = self.titleArr[indexPath.row];
        return cell;
    }
    if (indexPath.row == 2 || indexPath.row == 3) {
        PersonalInfoBottomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalInfoBottomTableViewCell class]) forIndexPath:indexPath];
        cell.model = self.titleArr[indexPath.row];
        if (indexPath.row == 2) {
            [cell.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell.contentView.mas_left);
                make.bottom.right.equalTo(cell.contentView);
                make.height.mas_offset(1/WIDTH_6_SCALE);
            }];
        }
        return cell;
    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 62/WIDTH_6_SCALE;
    }else{
        return 41/WIDTH_6_SCALE;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        PersonalInfoBottomTableViewCell * cell = (PersonalInfoBottomTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        [self.birthDayView show];
        self.birthDayView.getDateBlock = ^(NSString * dateString) {
            cell.contentLab.text = dateString;
        };
        
//        cell.contentLab.text = nowDateStr;
    }
    if (indexPath.row == 3) {
        PersonalInfoBottomTableViewCell * cell = (PersonalInfoBottomTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        [self.sexView show];
        
        self.sexView.getSexHanle = ^(NSString *string) {
            cell.contentLab.text = string;
        };
    }
}
#pragma mark - action
- (void)naviRightBtnClick:(UIButton *)sender{
    NSLog(@"跳过");
}
@end
